/*
 * Created by JFormDesigner on Sat Jun 05 23:15:04 CST 2021
 */

package com.J1ay.carsys.gui.Emp.Dialog;

import com.J1ay.carsys.gui.tishi.Success;
import com.J1ay.carsys.gui.tishi.Warning;
import com.J1ay.carsys.pojo.Bower;
import com.J1ay.carsys.pojo.Result;
import com.J1ay.carsys.service.EmpService;
import com.J1ay.carsys.service.Impl.EmpServiceImpl;
import com.J1ay.carsys.utils.Common;

import java.awt.*;
import java.awt.event.*;
import java.util.Date;
import javax.swing.*;
import javax.swing.GroupLayout;

/**
 * @author Brainrain
 */
public class AddBower extends JDialog {
    EmpService empService = new EmpServiceImpl();
    Bower bower = new Bower();
    String perm = "normal";
    int EmpId,len;
    public AddBower(Window owner,int EmpId,int len) {
        super(owner);
        initComponents();
        ButtonGroup group = new ButtonGroup();
        group.add(normal);
        group.add(vip);
        group.add(svip);
        perm = bower.getBperm();
        normal.isSelected();
        bower.setEmpId(EmpId);
        bower.setStatus(1);
        this.EmpId = EmpId;
        this.len = len;
    }
    public void start() {
        this.setVisible(true);
    }
    private void addBowerActionPerformed(ActionEvent e) {
        // TODO add your code here
        String phone = this.phone.getText();
        String Bname = this.Bname.getText();
        String Ecard = this.Ecard.getText();
        String regex = "\\d{15}(\\d{2}[0-9xX])?";
        System.out.println(Ecard);
        if(!Ecard.matches(regex)){
            new Warning(this,"身份证格式错误!").start();
            this.Ecard.setText("");
        }
        else if (!Common.checkPhone(phone)){
            new Warning(this,"手机号格式错误!").start();
            this.phone.setText("");
        }
        else {
            bower.setEmpId(this.EmpId);
            bower.setUId(String.format(""+"2021%04d", this.len+1));
            bower.setBname(Bname);
            bower.setEcard(Ecard);
            bower.setPhone(phone);
            bower.setBperm(perm);
            bower.setStatus(1);
            bower.setCreateTime(new Date());
            Result result = empService.addBower(bower);
            if (200 == result.getStatus()){
                new Success(this,"注册成功!").start();
                this.dispose();
            }
            else{
                new Warning(this,result.getMsg()).start();
            }
        }
    }

    private void normalActionPerformed(ActionEvent e) {
        // TODO add your code here
        perm = "normal";
    }

    private void vipActionPerformed(ActionEvent e) {
        // TODO add your code here
        perm = "vip";
    }

    private void svipActionPerformed(ActionEvent e) {
        // TODO add your code here
        perm = "svip";
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents
        label1 = new JLabel();
        label2 = new JLabel();
        label3 = new JLabel();
        label4 = new JLabel();
        Bname = new JTextField();
        Ecard = new JTextField();
        phone = new JTextField();
        addBower = new JButton();
        label5 = new JLabel();
        normal = new JRadioButton();
        vip = new JRadioButton();
        svip = new JRadioButton();

        //======== this ========
        setTitle("\u589e\u52a0\u6ce8\u518c\u79df\u8f66\u4eba\u4fe1\u606f");
        setResizable(false);
        Container contentPane = getContentPane();

        //---- label1 ----
        label1.setText("\u79df\u8f66\u4eba\u5458\u4fe1\u606f");

        //---- label2 ----
        label2.setText("\u59d3\u540d");

        //---- label3 ----
        label3.setText("\u8eab\u4efd\u8bc1\u53f7\u7801");

        //---- label4 ----
        label4.setText("\u624b\u673a\u53f7\u7801");

        //---- addBower ----
        addBower.setText("\u6ce8\u518c");
        addBower.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                addBowerActionPerformed(e);
            }
        });

        //---- label5 ----
        label5.setText("\u6743\u9650");

        //---- normal ----
        normal.setText("\u666e\u901a");
        normal.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                normalActionPerformed(e);
            }
        });

        //---- vip ----
        vip.setText("\u9ad8\u7ea7");
        vip.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                vipActionPerformed(e);
            }
        });

        //---- svip ----
        svip.setText("\u8d85\u7ea7");
        svip.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                svipActionPerformed(e);
            }
        });

        GroupLayout contentPaneLayout = new GroupLayout(contentPane);
        contentPane.setLayout(contentPaneLayout);
        contentPaneLayout.setHorizontalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addContainerGap()
                            .addComponent(label1, GroupLayout.PREFERRED_SIZE, 110, GroupLayout.PREFERRED_SIZE)
                            .addGap(0, 0, Short.MAX_VALUE))
                        .addGroup(contentPaneLayout.createSequentialGroup()
                            .addGap(54, 54, 54)
                            .addGroup(contentPaneLayout.createParallelGroup()
                                .addComponent(label2, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addComponent(label4, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .addGroup(contentPaneLayout.createSequentialGroup()
                                    .addComponent(label3, GroupLayout.PREFERRED_SIZE, 72, GroupLayout.PREFERRED_SIZE)
                                    .addGap(0, 0, Short.MAX_VALUE))
                                .addComponent(label5, GroupLayout.Alignment.TRAILING, GroupLayout.DEFAULT_SIZE, GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.LEADING, false)
                        .addComponent(Bname)
                        .addComponent(Ecard)
                        .addComponent(phone)
                        .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                            .addComponent(normal, GroupLayout.PREFERRED_SIZE, 70, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(vip, GroupLayout.PREFERRED_SIZE, 62, GroupLayout.PREFERRED_SIZE)
                            .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                            .addComponent(svip, GroupLayout.PREFERRED_SIZE, 66, GroupLayout.PREFERRED_SIZE)))
                    .addGap(44, 44, 44))
                .addGroup(GroupLayout.Alignment.TRAILING, contentPaneLayout.createSequentialGroup()
                    .addGap(0, 0, Short.MAX_VALUE)
                    .addComponent(addBower)
                    .addGap(154, 154, 154))
        );
        contentPaneLayout.setVerticalGroup(
            contentPaneLayout.createParallelGroup()
                .addGroup(contentPaneLayout.createSequentialGroup()
                    .addContainerGap()
                    .addComponent(label1, GroupLayout.PREFERRED_SIZE, 27, GroupLayout.PREFERRED_SIZE)
                    .addGap(12, 12, 12)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label2, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE)
                        .addComponent(Bname, GroupLayout.PREFERRED_SIZE, 33, GroupLayout.PREFERRED_SIZE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(label3, GroupLayout.PREFERRED_SIZE, 35, GroupLayout.PREFERRED_SIZE)
                        .addComponent(Ecard, GroupLayout.DEFAULT_SIZE, 41, Short.MAX_VALUE))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.UNRELATED)
                    .addGroup(contentPaneLayout.createParallelGroup()
                        .addComponent(label4, GroupLayout.PREFERRED_SIZE, 32, GroupLayout.PREFERRED_SIZE)
                        .addComponent(phone, GroupLayout.DEFAULT_SIZE, 40, Short.MAX_VALUE))
                    .addGap(18, 18, 18)
                    .addGroup(contentPaneLayout.createParallelGroup(GroupLayout.Alignment.BASELINE)
                        .addComponent(label5, GroupLayout.PREFERRED_SIZE, 31, GroupLayout.PREFERRED_SIZE)
                        .addComponent(normal)
                        .addComponent(vip)
                        .addComponent(svip))
                    .addPreferredGap(LayoutStyle.ComponentPlacement.RELATED, 12, Short.MAX_VALUE)
                    .addComponent(addBower)
                    .addGap(20, 20, 20))
        );
        pack();
        setLocationRelativeTo(getOwner());
        // JFormDesigner - End of component initialization  //GEN-END:initComponents
    }

    // JFormDesigner - Variables declaration - DO NOT MODIFY  //GEN-BEGIN:variables
    private JLabel label1;
    private JLabel label2;
    private JLabel label3;
    private JLabel label4;
    private JTextField Bname;
    private JTextField Ecard;
    private JTextField phone;
    private JButton addBower;
    private JLabel label5;
    private JRadioButton normal;
    private JRadioButton vip;
    private JRadioButton svip;
    // JFormDesigner - End of variables declaration  //GEN-END:variables
}
